package com.service.business.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.trading.controller.rep.TradingSpotPairsRep;
import com.service.business.trading.service.TradingSpotPairsService;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.TradingSpotPairsMapper;
import com.service.dao.model.TradingSpotOrder;
import com.service.dao.model.TradingSpotPairs;
import com.service.match.common.utils.TradingSpotUtils;
import com.service.utility.common.MathUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Service("tradingSpotPairsService")
public class TradingSpotPairsServiceImpl extends ServiceImpl<TradingSpotPairsMapper, TradingSpotPairs> implements TradingSpotPairsService {


    @Override
    public TradingSpotPairs getBySymbol(String symbol) {
        return getOne(new QueryWrapper<TradingSpotPairs>().lambda()
                .eq(TradingSpotPairs::getSymbol, symbol)
                .eq(TradingSpotPairs::getPairsStatus, CommonStatusEnums.ACTIVE.getCode())
                .last("limit 1"));
    }

    @Override
    public List<TradingSpotPairsRep> listPairs() {
        return list(new QueryWrapper<TradingSpotPairs>().lambda()
                .eq(TradingSpotPairs::getPairsStatus, CommonStatusEnums.ACTIVE.getCode()))
                .stream().map(item -> ModelMapperUtil.map(item, TradingSpotPairsRep.class))
                .collect(Collectors.toList());
    }

    @Override
    public BigDecimal computeTradeFee(TradingSpotOrder spotOrder, BigDecimal orderPrice, BigDecimal tradeNum) {
        TradingSpotPairs spotPairs = getBySymbol(spotOrder.getSymbol());
        if (TradingSpotUtils.isBuy(spotOrder.getTradeType())) {
            return MathUtil.mul(tradeNum,spotPairs.getBuyFee());
        }else{
            return orderPrice.multiply(tradeNum).multiply(spotPairs.getSellFee());
        }

    }
}
